package com.icebartech.base.modules.user.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.icebartech.base.modules.user.mapper.AdminUserCoreMapper;
import com.icebartech.base.modules.user.mapper.AdminUserLoginInfoMapper;
import com.icebartech.base.modules.user.mapper.AppUserRoleMapper;
import com.icebartech.base.modules.user.model.AdminUserCore;
import com.icebartech.base.modules.user.model.AdminUserLoginInfo;
import com.icebartech.base.modules.user.model.AppUserRole;
import com.icebartech.base.modules.user.service.AdminUserLoginInfoService;
import com.icebartech.core.modules.AbstractService;
import com.icebartech.core.modules.BaseMapper;
import com.icebartech.core.utils.CommonUtil;

@Service
public class AdminUserLoginInfoServiceImpl extends AbstractService implements AdminUserLoginInfoService {

	@Autowired
	private AdminUserLoginInfoMapper adminUserLoginInfoMapper;
	
	@Autowired
	private AdminUserCoreMapper  adminUserCoreMapper;
	
	@Autowired
	private AppUserRoleMapper appUserRoleMapper;
	
	@Override
	public BaseMapper getBaseMapper() {
		return this.adminUserLoginInfoMapper;
	}

	
	@Override
	public AdminUserLoginInfo findLoginInfoByName(String loginName) {
		return adminUserLoginInfoMapper.selectByLoginName(loginName);
	}
 
	/**
	 * 修改登录账户
	 */
	@Override
	public boolean updateAdnimUserLoginInfoBySelective(AdminUserCore adminUserCore,
			AdminUserLoginInfo adminUserLoginInfo, String roleKey) {
		// TODO Auto-generated method stub 
		//获取当前登录用户
		 //维护密码
		if (StringUtils.isNotBlank(adminUserLoginInfo.getPwd())) {
			String slat = RandomStringUtils.randomAlphanumeric(8);
			adminUserLoginInfo.setPwd2(slat);
			adminUserLoginInfo.setPwd(CommonUtil.pwdMd5(adminUserLoginInfo.getPwd().trim(), slat));
		} 
		adminUserLoginInfo.setGmtModified(new Date());
		adminUserLoginInfo.setModifier(adminUserCore.getUserId().toString());
		//修改登录账户
		int updateLoginInfo = adminUserLoginInfoMapper.updateByPrimaryKeySelective(adminUserLoginInfo);
		//修改用户信息
		adminUserCore.setGmtCreated(new Date());
		adminUserCore.setModifier(adminUserCore.getUserId().toString());
		int updateUserCore = adminUserCoreMapper.updateByPrimaryKeySelective(adminUserCore);
		//修改账户所属角色
		int updateUserRole = 0;
		if (StringUtils.isNotBlank(roleKey)) {
			AppUserRole record = new AppUserRole();
			record.setUserId(adminUserLoginInfo.getUserId());
			List<AppUserRole> userRoles = appUserRoleMapper.selectBySelective(record);
				if (userRoles != null && userRoles.size() > 0 ) {
					AppUserRole appUserRole = userRoles.get(0);
					appUserRole.setRoleKey(roleKey);
					appUserRole.setGmtModified(new Date());  
					updateUserRole = appUserRoleMapper.updateByPrimaryKeySelective(appUserRole);
				}
		} 
		if (updateLoginInfo > 0 && updateUserCore > 0 && updateUserRole > 0) {
			return true;
		} 
		return false;
	}


	/**
	 * 新增账户
	 */
	@Override
	public boolean registerAdminUserLoginInfor(AdminUserCore adminUserCore, AdminUserLoginInfo adminUserLoginInfo,
			String roleKey) {
		//获取当前登录用户
		//新增用户基本信息
		String creator = adminUserCore.getCreator().toString();
		adminUserCore.setGmtCreated(new Date());
		adminUserCore.setGmtModified(new Date());
		adminUserCore.setCreator(creator);
		adminUserCore.setModifier(creator); 
		if ((adminUserCoreMapper.insertSelective(adminUserCore)) > 0) {
			boolean flag = false ;
			AdminUserCore selectByUserName = adminUserCoreMapper.selectByUserName(adminUserCore.getUserName());
			//新增登录用户信息
			adminUserLoginInfo.setUserId(selectByUserName.getUserId());
			adminUserLoginInfo.setCreator(creator);
			adminUserLoginInfo.setModifier(creator);
			adminUserLoginInfo.setGmtModified(new Date());
			adminUserLoginInfo.setGmtCreated(new Date());
			adminUserLoginInfo.setLoginCount(0); 
			String slat = RandomStringUtils.randomAlphanumeric(8);
			adminUserLoginInfo.setPwd2(slat);
			adminUserLoginInfo.setPwd(CommonUtil.pwdMd5(adminUserLoginInfo.getPwd().trim(), slat));
			if(adminUserLoginInfoMapper.insertSelective(adminUserLoginInfo) > 0) {
				flag = true;
			}
			//维护角色
			if (StringUtils.isNotBlank(roleKey)) {
				AppUserRole record = new AppUserRole();
				record.setUserId(selectByUserName.getUserId());
				record.setRoleKey(roleKey);
				record.setCreator(creator);
				record.setModifier(creator);
				record.setGmtCreated(new Date());
				record.setGmtModified(new Date()); 
				if ((appUserRoleMapper.insertSelective(record)) > 0) {
					flag = true;
				}
			} 
			return flag; 
		} 
		return false;
	}
    
    public boolean updateUserPassword(Long userId, String pwd) {
        AdminUserLoginInfo adminUserLoginInfo = new AdminUserLoginInfo();
        adminUserLoginInfo.setUserId(userId);
        String slat    = RandomStringUtils.randomAlphanumeric(8);
        String pwd_new = CommonUtil.pwdMd5(pwd.trim(), slat);
        adminUserLoginInfo.setPwd2(slat);
        adminUserLoginInfo.setPwd(pwd_new);
        adminUserLoginInfo.setGmtModified(new Date());
        //修改登录账户
        return adminUserLoginInfoMapper.updateByPrimaryKeySelective(adminUserLoginInfo) > 0;
    }
}
